package com.burgeon.backend.SHOP.service.Impl;
/**
 * 小程序消息模板
 */

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.burgeon.backend.SHOP.mappers.RefundMapper;
import com.burgeon.backend.SHOP.mappers.ShopTempateMapper;
import com.burgeon.backend.SHOP.mappers.WechatAppMapper;
import com.burgeon.backend.SHOP.service.ShopMessageTemplateService;
import com.burgeon.backend.WeChat.WeChatMessageConnector;
import com.burgeon.backend.service.WechatService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Log
public class ShopMessageTemplateServiceImpl implements ShopMessageTemplateService {

    @Autowired
    private WechatService wechatService;
    @Autowired
    private RefundMapper refundMapper;
    @Autowired
    private WechatAppMapper wechatAppMapper;
    @Autowired
    private WeChatMessageConnector weChatMessageConnector;
    @Autowired
    private ShopTempateMapper shopTempateMapper;

    public void sendOrderDelivery(String orderId, String orderState, String date,String address,String openid,String token){
        String accessToken = wechatService.GetAccesTokenByAccount(token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
        String templateId = shopTempateMapper.findTemplateOrderDelivery(token);
        String value="您好，您有一个订单已经发货，请及时查看并收货";
        String data = MessageOrderDelivery(value,orderId,orderState,date,address,openid,templateId);
        String str = weChatMessageConnector.Send(url, data);
        System.err.println(str);

    }

    //尾款支付
    public void sendFinalPayMessageTemplate(String goodsName,String finalMoney,String endTime,String openid,String token){
        String accessToken = wechatService.GetAccesTokenByAccount(token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
        String templateId = shopTempateMapper.findTemplateFinalPay(token);
        String value="您有一笔订单尾款待支付";
        String data = MessageFinalPay(wechatAppMapper.getWechat(token).getAppid(),value,goodsName,finalMoney,endTime,openid,templateId);
        String str = weChatMessageConnector.Send(url, data);
        System.err.println(str);

    }

    @Override
    public void sendRefundMessageTemplate(String value, String ordierId, String price, String openid, String token) {
        String accessToken = wechatService.GetAccesTokenByAccount(token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
        String templateId = refundMapper.getRefundTemplate(token);
        //Wechat wechat = wechatService.GetWechatByAccount(token);
        wechatAppMapper.getWechat(token);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd" + " " + "HH:mm:ss");

        String data = MessageDataForRefund(value, ordierId, price, sdf.format(new Date()), openid, templateId, wechatAppMapper.getWechat(token).getAppid());

        String str = weChatMessageConnector.Send(url, data);
        System.err.println(str);

    }
    private String MessageOrderDelivery(String value, String orderId, String orderState, String date,String address,
                                   String openid, String template) {

        return "{  \"touser\":\"" + openid + "\",\n" +
                "           \"template_id\":\"" + template + "\",\n" +
                "           \"data\":{\n" +
                "                   \"first\": {\n" +
                "                       \"value\":\"" + value + "\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword1\":{\n" +
                "                       \"value\":\"" + orderId + "\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword2\": {\n" +
                "                       \"value\":\"" + orderState + "\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword3\": {\n" +
                "                       \"value\":\"" + date + "\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword3\": {\n" +
                "                       \"value\":\"" + address + "\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"remark\":{\n" +
                "                       \"value\":\"进入系统 查看详情\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   } }   }";
    }

    private String MessageFinalPay(String appid,String value, String goodsName, String finalMoney, String endTime,
                                        String openid, String template) {

        return "{  \"touser\":\"" + openid + "\",\n" +
                "           \"template_id\":\"" + template + "\",\n" +
                " \"miniprogram\":{\n" +
                "             \"appid\":\"" + appid + "\",\n" +
                "             \"pagepath\":\"'/pages/userStart/userStart?empName=''&linkpage='orderList'\"\n" +
                "           },\n " +
                "           \"data\":{\n" +
                "                   \"first\": {\n" +
                "                       \"value\":\"" + value + "\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword1\":{\n" +
                "                       \"value\":\"" + goodsName + "\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword2\": {\n" +
                "                       \"value\":\"" + finalMoney + "\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword3\": {\n" +
                "                       \"value\":\"" + endTime + "\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"remark\":{\n" +
                "                       \"value\":\"点击支付\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   } }   }";
    }


    private String MessageDataForRefund(String value, String orderId, String price, String date,
                                        String openid, String template, String appid) {

        return "{  \"touser\":\"" + openid + "\",\n" +
                "           \"template_id\":\"" + template + "\",\n" +
                " \"miniprogram\":{\n" +
                "             \"appid\":\"" + appid + "\",\n" +
                "             \"pagepath\":\"/refund/pages/refundStart/refundStart\"\n" +
                "           },\n " +
                "           \"data\":{\n" +
                "                   \"first\": {\n" +
                "                       \"value\":\"" + value + "\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword1\":{\n" +
                "                       \"value\":\"" + orderId + "\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword2\": {\n" +
                "                       \"value\":\"" + price + "\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword3\": {\n" +
                "                       \"value\":\"" + date + "\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"remark\":{\n" +
                "                       \"value\":\"感谢您对我们的支持，欢迎您下次光临！\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   } }   }";
    }

    //xx天未评价
    @Override
    public void sendOrderRate( String openid, String orderId,
                                String token,String price,String goodsName) {
        String accessToken = wechatService.GetAccesTokenByAccount(token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
        String templateId =shopTempateMapper.findTemplate5(token);
//        templateId="tLTJ8pok3fSV7snEsU4S2L40e9JTOytTb7RkVIkrgxs";
//        System.err.println(templateId);
        String value1 = "您好，您有订单"+goodsName+"还未评价";
//        openid="oqjs35ynLXNSyO2WDntf3qaIPGKc";
        String data = sendMessageStore(openid,orderId,templateId,value1,price);
        String str = weChatMessageConnector.Send(url, data);
        JSONObject jsonObject = JSONArray.parseObject(str);
//        System.err.println(str);
        log.info("发送评价提醒"+str);

    }

    @Override
    public void sendBargain( String openid,String token,String finishTime,String goodsName) {
        System.err.println("hvjdjfk");
        String accessToken = wechatService.GetAccesTokenByAccount(token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
        String templateId =shopTempateMapper.findTemplate7(token);
//        templateId="uB7BU8QiAYJ2FYQ7akynhQoTK0Pqk80Q8whFNCs2zHA";
        System.err.println(templateId);
        String value1 = "恭喜您，"+goodsName+",砍价成功~";
        openid="oqjs35ynLXNSyO2WDntf3qaIPGKc";
        String data = sendMessageFinish2(openid,templateId,value1,finishTime,goodsName);
        String str = weChatMessageConnector.Send(url, data);
        log.info("发送砍价成功提醒"+str);

    }

    @Override
    public void sendGroup( String openid,String orderId,String token,String finishTime,String goodsName) {
        String accessToken = wechatService.GetAccesTokenByAccount(token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
        String templateId =shopTempateMapper.findTemplate6(token);
//        templateId="Wdr2DUBKdj4ZHmRO4dBmIjzeA-DJ07ydhoqdEbAf6IA";
//        System.err.println(templateId);
        String value1 = "恭喜您，"+goodsName+",拼团成功啦~";
        openid="oqjs35ynLXNSyO2WDntf3qaIPGKc";
        String data = sendMessageFinish(openid,orderId,templateId,value1,finishTime,goodsName);
        String str = weChatMessageConnector.Send(url, data);
        log.info("发送拼团成功提醒"+str);

    }

    private String sendMessageFinish(String openid,String orderId, String templateId,
                                    String value1,String finishTime,String goodsName){
        return "{  \"touser\":\"" + openid + "\",\n" +
                "           \"template_id\":\"" + templateId + "\",\n" +
                "           \"data\":{\n" +
                "                   \"first\": {\n" +
                "                       \"value\":\""+value1+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword1\":{\n" +
                "                       \"value\":\""+orderId+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword2\": {\n" +
                "                       \"value\":\""+finishTime+"\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"remark\":{\n" +
                "                       \"value\":\""+"感谢您对我们的支持，欢迎您下次光临！"+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   } }   }";

    }
    private String sendMessageFinish2(String openid,String templateId,String value1,String finishTime,String goodsName){
        return "{  \"touser\":\"" + openid + "\",\n" +
                "           \"template_id\":\"" + templateId + "\",\n" +
                "           \"data\":{\n" +
                "                   \"first\": {\n" +
                "                       \"value\":\""+value1+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword1\":{\n" +
                "                       \"value\":\""+goodsName+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword2\": {\n" +
                "                       \"value\":\""+finishTime+"\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"remark\":{\n" +
                "                       \"value\":\""+"感谢您对我们的支持，欢迎您下次光临！"+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   } }   }";

    }

    private String sendMessageStore(String openid,String orderId, String templateId,
                                    String value1,String price){
        return "{  \"touser\":\"" + openid + "\",\n" +
                "           \"template_id\":\"" + templateId + "\",\n" +
                "           \"data\":{\n" +
                "                   \"first\": {\n" +
                "                       \"value\":\""+value1+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword1\":{\n" +
                "                       \"value\":\""+orderId+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword2\": {\n" +
                "                       \"value\":\""+price+"\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"remark\":{\n" +
                "                       \"value\":\""+"进入系统 查看详情"+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   } }   }";

    }
}
